<?php
class CartForm extends Form
{
	private $cart=false,$cart_products=false;
	
	function CartForm()
	{
		Form::Form('CartForm');
		
		CGlobal::$website_title = "Giỏ hàng";
		
		$this->link_js('javascript/aznet/cart.js');
	}
	
	function on_submit()
	{
		if(Cart::$cart && Cart::$cart_items)
		{
			$quantitys	=	Url::get('quantity');
			$checkeds	=	Url::get('checked');
			
			$cart_price 	= 0;
			$cart_quantity 	= 0;
					
				
			if($quantitys)
			{
				$sql_arr=array();
				
				foreach ($quantitys as $id	=>	$quantity)
				{
					if($quantity < 1)
					{
						$this->setFormError('quantity',"Số lượng sản phẩm phải là một số nguyên lớn hơn 0 ");
						return ;
					}
					
					
					
					$checked = (int)@$checkeds[$id];
					
					if(Cart::$cart_items[$id]['quantity'] != $quantity || Cart::$cart_items[$id]['checked'] != $checked)
					{
						$sql_arr[]	=	"UPDATE cart_item SET quantity=".(int)$quantity.", checked=$checked WHERE id=".$id;
					}
					
					if($checked)
					{
						$cart_price 	+= Cart::$cart_items[$id]['price'] * $quantity;
						$cart_quantity	+= $quantity;
					}
				}
				
				if($sql_arr)
				{
					foreach ($sql_arr as $query)
					{
						DB::query($query);
					}
					
					DB::update("cart",array("time_change"=>TIME_NOW),"id=".Cart::$cart['id']);
				}
			}
			
			if(Cart::$cart['price'] != $cart_price || Cart::$cart['quantity'] != $cart_quantity)
			{
				DB::update("cart",array('price'=>$cart_price,'quantity'=>$cart_quantity),'id='.Cart::$cart['id']);
			}
		}
		
		if(Url::get('cart_act') =='finish')
		{
			Url::redirect_current(array('cmd'=>'finish'));
		}
		
		URL::redirect_current();
	}
	
	function draw()
	{
		global $display;
		
		$display->add('msg',$this->showFormErrorMessages(1));
		
		$quantity_total	=0;
		$price_total	=0;
			
		$cart_items = array();
		
		if(Cart::$cart_items)
		{
			foreach (Cart::$cart_items  as $cart_item_id => $cart_item)
			{
				$item = Cart::$items[$cart_item['item_id']];
				
				if(isset(CGlobal::$allCategories[$item['category_id']]))
				{
					$cart_item['url'] = WEB_DIR.CGlobal::$allCategories[$item['category_id']]['nice_name']."/p{$item['id']}/".AZLib::safe_title($item['name']).".html";
				}
				else{	
					$cart_item['url'] = WEB_DIR."p{$item['id']}/".AZLib::safe_title($item['name']).".html";
				}
		
				$cart_item['name'] 		= $item['name'];
				$cart_item['price_num'] = $item['price'];
				
				$cart_item['price'] 	= number_format($item['price'],0,',','.');
				
				if ($item['img_url']){
					$cart_item['img_url'] 		= AZLib::getImageThumb($item['img_url'],120,120,0,$item['img_server']);
				}
				else{
					$item['img_url'] 			=  '';
				}
				
				$cart_item['row_total'] = $cart_item['checked'] ? (number_format($item['price'] * $cart_item['quantity'],null,null,'.')) . ' VNĐ' : '';
				
				$cart_item['delete'] 	= Url::build_current(array('cmd'=>'delete','id'=>$cart_item['id']));
				
				$price_total	+=	$cart_item['checked'] ? ($item['price'] * $cart_item['quantity']) : 0;
				$quantity_total	+=	$cart_item['checked'] ? $cart_item['quantity'] : 0;
				
				$cart_items[$cart_item['id']] = $cart_item;
			}
		}
			
		$display->add('quantity_total',number_format($quantity_total,null,null,'.'));
		$display->add('price_total', number_format($price_total,null,null,'.'));
		
		$display->add('url_finish',Url::build_current(array('cmd'=>'finish')));
		$display->add('cart_items',$cart_items);
		
		$display->add("print_link", Url::open_popup(Url::build('cart_print',array('id'=> Cart::$cart['id'])),1000,800,false,false,false,false,false,1,1));	
		
		$this->beginForm();
		$display->output('Cart');
		$this->endForm();
	}
}
?>